package com.factory.iamp.service.impl;

import com.factory.iamp.dao.GroupMapper;
import com.factory.iamp.dao.UserMapper;
import com.factory.iamp.pojo.Group;
import com.factory.iamp.pojo.UserGroup;
import com.factory.iamp.pojo.UserList;
import com.factory.iamp.service.GroupService;
import com.factory.iamp.utils.exception.APIException;
import com.factory.iamp.utils.helper.PageHelper;
import com.factory.iamp.utils.helper.ResponseCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Throwable.class)
public class GroupServiceImpl implements GroupService {
    @Resource
    private GroupMapper groupMapper;
    @Resource
    private UserMapper userMapper;

    private void checkMemberList(List<String> member) {
        if(member.size() < 1) throw new APIException(ResponseCode.VALIDATE_FAILED, "成员列表为空");
        if(userMapper.listCount(member) != member.size())
            throw new APIException(ResponseCode.DATABASE_FAILED, "含有错误的用户id");
    }

    @Override
    public int Count() {
        return groupMapper.Count();
    }

    @Override
    public int groupMemberCount(int gid) {
        return groupMapper.groupMemberCount(gid);
    }

    @Override
    public int findByNameCount(String name) {
        return groupMapper.findByNameCount(name);
    }

    @Override
    public List<Group> findByNameListPaged(PageHelper data, String name) {
        return groupMapper.findByNameListPaged(data.calOffset(), data.getLimit(), name);
    }

    @Override
    public List<Group> ListPaged(PageHelper data) {
        return groupMapper.ListPaged(data.calOffset(), data.getLimit());
    }

    @Override
    public List<Group> findByUid(String uid) {
        return groupMapper.findByUid(uid);
    }

    @Override
    public List<UserList> groupMemberListPaged(PageHelper data, int gid) {
        return groupMapper.groupMemberListPaged(data.calOffset(), data.getLimit(), gid);
    }

    @Override
    public void joinGroup(String uid, int gid) {
        if(groupMapper.findById(gid) == null) throw new APIException(ResponseCode.DATABASE_FAILED, "分组不存在");
        if(groupMapper.checkDuplicate(gid, uid) != null)
            throw new APIException(ResponseCode.DATABASE_FAILED, "已加入小组");
        groupMapper.addUser(gid, uid);
        groupMapper.addCount(gid);
    }

    @Override
    public void deleteGroup(int gid) {
        if(groupMapper.findById(gid) == null) throw new APIException(ResponseCode.DATABASE_FAILED, "分组不存在");
        groupMapper.deleteRelation(gid);
        groupMapper.deleteGroup(gid);
    }

    @Override
    public void makeGroup(String name, List<String> member) {
        checkMemberList(member);
        Group group = groupMapper.findByName(name);
        if(group != null) throw new APIException("分组已存在");
        group = new Group(0, name, member.size());
        groupMapper.addGroup(group);
        int gid = group.getGid();
        List<UserGroup> gid_uid = new ArrayList<>();
        for (String s : member) gid_uid.add(new UserGroup(gid, s));
        groupMapper.addUserList(gid_uid);
    }

    @Override
    public void alterGroup(int gid, List<String> member) {
        checkMemberList(member);
        Group group = groupMapper.findById(gid);
        if(group == null) throw new APIException("分组不存在");
        groupMapper.deleteRelation(gid);
        List<UserGroup> gid_uid = new ArrayList<>();
        for (String s : member) gid_uid.add(new UserGroup(gid, s));
        groupMapper.addUserList(gid_uid);
        groupMapper.alterCount(gid, member.size());
    }
}
